package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.Map;


@Activate(group = {CommonConstants.CONSUMER, CommonConstants.PROVIDER})
public class DubboInvokeFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long startTime = System.currentTimeMillis();
        try {
            boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
            if (isConsumerSide) {
                System.out.println("当前为客户端");
                Map<String, String> stringStringMap = invocation.getAttachments();
                // 设置参数
                stringStringMap.put("ipAddress", RpcContext.getContext().getLocalHost());
                // 执行方法
                return invoker.invoke(invocation);
            }
            boolean isProviderSide = RpcContext.getContext().isProviderSide();
            if (isProviderSide) {
                System.out.println("当前为服务端");
                return invoker.invoke(invocation);
            }
        }catch (RpcException e) {
            System.out.println(e.getMessage());
            return invoker.invoke(invocation);
        }
        return invoker.invoke(invocation);
    }
}
